<HTML>
<BODY>
<PRE>
<!-- Manpage converted by man2html 3.0.1 -->

<B><A HREF="QSORT.html">QSORT(3)</A></B>	       FreeBSD Library Functions Manual 	      <B><A HREF="QSORT.html">QSORT(3)</A></B>


</PRE>
<H2>NAME</H2><PRE>
     <B>qsort,</B> <B>heapsort,</B> <B>mergesort</B> - sort functions


</PRE>
<H2>SYNOPSIS</H2><PRE>
     <B>#include</B> <B>&lt;stdlib.h&gt;</B>

     <I>void</I>
     <B>qsort</B>(<I>void</I> <I>*base</I>, <I>size</I><B>_</B><I>t</I> <I>nmemb</I>, <I>size</I><B>_</B><I>t</I> <I>size</I>,
	     <I>int</I> <I>(*compar)(const</I> <I>void</I> <I>*,</I> <I>const</I> <I>void</I> <I>*)</I>)

     <I>int</I>
     <B>heapsort</B>(<I>void</I> <I>*base</I>, <I>size</I><B>_</B><I>t</I> <I>nmemb</I>, <I>size</I><B>_</B><I>t</I> <I>size</I>,
	     <I>int</I> <I>(*compar)(const</I> <I>void</I> <I>*,</I> <I>const</I> <I>void</I> <I>*)</I>)

     <I>int</I>
     <B>mergesort</B>(<I>void</I> <I>*base</I>, <I>size</I><B>_</B><I>t</I> <I>nmemb</I>, <I>size</I><B>_</B><I>t</I> <I>size</I>,
	     <I>int</I> <I>(*compar)(const</I> <I>void</I> <I>*,</I> <I>const</I> <I>void</I> <I>*)</I>)


</PRE>
<H2>DESCRIPTION</H2><PRE>
     The <B>qsort</B>() function is a modified partition-exchange sort, or quicksort.
     The <B>heapsort</B>() function is a modified selection sort.  The <B>mergesort</B>()
     function is a modified merge sort with exponential search intended for
     sorting data with pre-existing order.

     The <B>qsort</B>() and <B>heapsort</B>() functions sort an array of <I>nmemb</I> objects, the
     initial member of which is pointed to by <I>base</I>. The size of each object is
     specified by <I>size</I>. <B>Mergesort</B>() behaves similarly, but <I>requires</I> that <I>size</I>
     be greater than ``sizeof(void *) / 2''.

     The contents of the array <I>base</I> are sorted in ascending order according to
     a comparison function pointed to by <I>compar</I>, which requires two arguments
     pointing to the objects being compared.

     The comparison function must return an integer less than, equal to, or
     greater than zero if the first argument is considered to be respectively
     less than, equal to, or greater than the second.

     The functions <B>qsort</B>() and <B>heapsort</B>() are <I>not</I> stable, that is, if two mem-
     bers compare as equal, their order in the sorted array is undefined.  The
     function <B>mergesort</B>() is stable.

     The <B>qsort</B>() function is an implementation of C.A.R. Hoare's ``quicksort''
     algorithm, a variant of partition-exchange sorting; in particular, see
     D.E. Knuth's Algorithm Q.	<B>Qsort</B>() takes O N lg N average time.  This im-
     plementation uses median selection to avoid its O N**2 worst-case behav-
     ior.

     The <B>heapsort</B>() function is an implementation of J.W.J. William's ``heap-
     sort'' algorithm, a variant of selection sorting; in particular, see D.E.
     Knuth's Algorithm H.  <B>Heapsort</B>() takes O N lg N worst-case time.  Its
     <I>only</I> advantage over <B>qsort</B>() is that it uses almost no additional memory;
     while <B>qsort</B>() does not allocate memory, it is implemented using recur-
     sion.

     The function <B>mergesort</B>() requires additional memory of size <I>nmemb</I> <I>*</I> <I>size</I>
     bytes; it should be used only when space is not at a premium.
     <B>Mergesort</B>() is optimized for data with pre-existing order; its worst case
     time is O N lg N; its best case is O N.

     Normally, <B>qsort</B>() is faster than <B>mergesort</B>() is faster than <B>heapsort</B>().
     Memory availability and pre-existing order in the data can make this un-
     true.


</PRE>
<H2>RETURN VALUES</H2><PRE>
     The <B>qsort</B>() function returns no value.

     Upon successful completion, <B>heapsort</B>() and <B>mergesort</B>() return 0.  Other-
     wise, they return -1 and the global variable <I>errno</I> is set to indicate the
     error.


</PRE>
<H2>ERRORS</H2><PRE>
     The <B>heapsort</B>() and <B>mergesort</B>() functions succeed unless:

     [EINVAL]	   The <I>size</I> argument is zero, or, the <I>size</I> argument to
		   <B>mergesort</B>() is less than ``sizeof(void *) / 2''.

     [ENOMEM]	   <B>Heapsort</B>() or <B>mergesort</B>() were unable to allocate memory.


</PRE>
<H2>COMPATIBILITY</H2><PRE>
     Previous versions of <B>qsort</B>() did not permit the comparison routine itself
     to call <B><A HREF="qsort.html">qsort(3)</A></B>.	This is no longer true.


</PRE>
<H2>SEE ALSO</H2><PRE>
     <B><A HREF="sort.html">sort(1)</A></B>,  <B><A HREF="radixsort.html">radixsort(3)</A></B>

     Hoare, C.A.R., "Quicksort", <I>The</I> <I>Computer</I> <I>Journal</I>, 5:1, pp. 10-15, 1962.

     Williams, J.W.J, "Heapsort", <I>Communications</I> <I>of</I> <I>the</I> <I>ACM</I>, 7:1, pp. 347-348,
     1964.

     Knuth, D.E., "Sorting and Searching", <I>The</I> <I>Art</I> <I>of</I> <I>Computer</I> <I>Programming</I>,
     Vol. 3, pp. 114-123, 145-149, 1968.

     Mcilroy, P.M., "Optimistic Sorting and Information Theoretic Complexity",
     <I>Fourth</I> <I>Annual</I> <I>ACM-SIAM</I> <I>Symposium</I> <I>on</I> <I>Discrete</I> <I>Algorithms</I>, January 1992.

     Bentley, J.L., "Engineering a Sort Function", <I>bentley@research.att.com</I>,
     January 1992.


</PRE>
<H2>STANDARDS</H2><PRE>
     The <B>qsort</B>() function conforms to ISO 9899: 1990 (``ISO C'').

BSD				 June 4, 1993				     2
</PRE>
<HR>
<ADDRESS>
Man(1) output converted with
<a href="http://www.oac.uci.edu/indiv/ehood/man2html.html">man2html</a>
</ADDRESS>
</BODY>
</HTML>
